<?php

namespace App\Models;

class Feedbacks extends Base
{
    protected $table = 'feedbacks';


    const STATUS_0 = 0;
    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_3 = 3;
    
    const STATUS_TEXT = [
    	self::STATUS_0 => '<span class="el-tag ">未处理</span>',
    	self::STATUS_1 => '<span class="el-tag el-tag--danger">未处理</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--info">任务通过</span>',
        self::STATUS_3 => '<span class="el-tag el-tag--danger">任务不通过</span>',
    ];

  

    protected $fillable = [
        'id','user_id','user_name','content','images','created_at',
        'reward_id','order_id','status','updated_at'
    ];
    
    
    
    protected $casts = ['images' => 'array'];
    
    
    public function reward_id(){//一级分类
    	return $this->belongsTo('App\Models\Demands', 'reward_id', 'id');
    }
    
    public function order_id(){//一级分类
    	return $this->belongsTo('App\Models\rewardOrder', 'order_id', 'id');
    }
    
    public function user_id(){
    	return $this->belongsTo('App\Models\Users', 'user_id', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
    //	var_dump($params);
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('user_name','like','%' . $params['kwd'] . '%')
                           ->orwhere('admin_name','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['status']) && $params['status']>0) {
                    $query ->where('status',$params['status']);
                }
               
            })->where('status','<>',-1)
            ->with(['reward_id' => function ($query) {
                $query->select(['id','title','user_name','status'])->get();
            }])->with(['order_id' => function ($query) {
                $query->select(['id','updated_at','end_time','get_money'])->get();
            }])->with(['user_id' => function ($query) {
                $query->select(['id','mobile','reward_num'])->get();
            }])->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
           
        ]);

       /// var_dump($lists);
        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = null, $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        if(isset($page)){
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                })->where('status','<>',-1)
                ->with(['reward_id' => function ($query) {
	                $query->select(['title','user_name','status'])->get();
	            }])->with(['order_id' => function ($query) {
	                $query->select(['updated_at','end_time','get_money'])->get();
	            }])->with(['user_id' => function ($query) {
	                $query->select(['mobile','reward_num'])->get();
	            }])
                ->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        }else{
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                })->where('status','<>',-1)
                ->with(['reward_id' => function ($query) {
                	$query->select(['title','user_name','status'])->get();
                }])->with(['order_id' => function ($query) {
                	$query->select(['updated_at','end_time','get_money'])->get();
                }])->with(['user_id' => function ($query) {
	                $query->select(['mobile','reward_num'])->get();
	            }])->orderBy($sort, $orderby)->get();
        }
        return $lists;
    }

    /**
     * 根据id查询一条信息
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function details($params, $fields = ['*']){
        return $this->select($fields)->where($params)->where('status','<>',-1)->first();
    }

}
